#include "fs_algo.h"
#include "Data.h"
#include "Extractor.h"
#include "GlobalExtractor.h"
#include "ResourceExtractor.h"
#include "BeamSearcher.h"
#include "Model.h"
using namespace std;

int main(int argc,char** argv)
{
	if(argc != 8) 
	{
		cerr << "Usage : ./this config model input output is-global-f resource-dir morph-path" << endl;
		exit(1);
	}

	Config config(argv[1]);

	CModel model(&config);
	model.loadMaxentModel(argv[2]);

	Extractor extractor;
	GlobalExtractor* ge = NULL;
	ResourceExtractor* re = NULL;

	if(strcmp(argv[5],"true") == 0) 
	{
		ge = new GlobalExtractor();
	}
	if(strcmp(argv[6],"NONE") != 0)
	{
		re = new ResourceExtractor(argv[6]);
	}
	if(strcmp(argv[7],"NONE") != 0)
	{
		morph = new CMorphology(argv[7]);
	}

	enumer = new Enumerable();
	former = new Former();

	BeamSearcher* searcher;
	searcher = new BeamSearcher(&config,&model,&extractor,ge,re);

	ifstream ifs(argv[3]);
	ofstream ofs(argv[4]);

	string line;
	int count = 0;

	while(getline(ifs,line))
	{
		line = fs_algo::strip(line);
		if(line.empty())
		{
			ofs << endl;
			continue;
		}

		vector<string> nbest;
		string best;
		searcher->search(line,best,nbest);

		// output 1best
		ofs << best << endl;
/*

		// output nbest
		for(int i=0; i<(int)nbest.size(); i++)
			ofs << nbest[i] << endl;
		ofs << endl << endl;
*/

		count++;
		if(count%100 == 0) cout << count << endl;
	}

	ifs.close();
	ofs.close();

	return 0;
}
